//
//  CZStatusToolBar.swift
//  weibo
//
//  Created by Apple on 16/3/16.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  首页微博cell上的toolBar
class CZStatusToolBar: UIView {

    private lazy var retweetButton: UIButton = self.addChildButton("转发", imgName: "timeline_icon_retweet")
    private lazy var commentButton: UIButton = self.addChildButton("评论", imgName: "timeline_icon_comment")
    private lazy var unlikeButton: UIButton = self.addChildButton("赞", imgName: "timeline_icon_unlike")
    
    var statusViewModel: CZStatusViewModel? {
        didSet {
            retweetButton.setTitle(statusViewModel?.retweetCountString, forState: .Normal)
            commentButton.setTitle(statusViewModel?.commentCountString, forState: .Normal)
            unlikeButton.setTitle(statusViewModel?.unLikeCountString, forState: .Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    private func addChildButton(title: String, imgName: String) -> UIButton {
        let button = UIButton()
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Highlighted)
        button.setImage(UIImage(named: imgName), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        addSubview(button)
        return button
    }
    
    private func addSeparatorLineView() -> UIView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //  添加控件
        
        let sp1 = addSeparatorLineView()
        let sp2 = addSeparatorLineView()
        //  设置约束
        retweetButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        
        commentButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetButton.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton)
        }
        
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
        }
        
        sp1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton.snp_trailing)
            make.centerY.equalTo(self)
           // make.centerY.equalTo(self.frame.height / 2)
        }
        sp2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(self)
           // make.centerY.equalTo(self.frame.height / 2)

        }
    }

}
